package org.zizi.db.tool.impl;

import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;

import org.zizi.db.tool.AbstractEasySqlTool;

public class PostgresqlEasySqlTool extends AbstractEasySqlTool
{
	public Object strings(Collection<String> ss)
	{
		return new PostgresqlStringArray(ss);
	}

	public Object longs(Collection<Long> ll)
	{
		return new PostgresqlLongArray(ll);
	}

	public Object integers(Collection<Integer> ii)
	{
		return new PostgresqlIntegerArray(ii);
	}
	
	/////////////
	
	public abstract static class PostgresqlArray<T> implements Array
	{
		protected String sql;
		
		public PostgresqlArray(Collection<T> list)
		{
			String d = "";
			StringBuffer sb = new StringBuffer("{");
			for (T x : list)
			{
				sb.append(d);
				sb.append(toString(x));

				d = ",";
			}
			sb.append("}");

			sql = sb.toString();
		}
		
		protected abstract String toString(T t);
		
		public String toString()
		{
			return sql;
		}
		
		public Object getArray() throws SQLException
		{
			throw new RuntimeException("没有实现");
		}

		public Object getArray(Map<String, Class<?>> map) throws SQLException
		{
			throw new RuntimeException("没有实现");
		}

		public Object getArray(long index, int count) throws SQLException
		{
			throw new RuntimeException("没有实现");
		}

		public Object getArray(long index, int count, Map<String, Class<?>> map) throws SQLException
		{
			throw new RuntimeException("没有实现");
		}

		public ResultSet getResultSet() throws SQLException
		{
			throw new RuntimeException("没有实现");
		}

		public ResultSet getResultSet(Map<String, Class<?>> map) throws SQLException
		{
			throw new RuntimeException("没有实现");
		}

		public ResultSet getResultSet(long index, int count) throws SQLException
		{
			throw new RuntimeException("没有实现");
		}

		public ResultSet getResultSet(long index, int count, Map<String, Class<?>> map) throws SQLException
		{
			throw new RuntimeException("没有实现");
		}

		public void free() throws SQLException
		{
			throw new RuntimeException("没有实现");
		}
	}

	public final static class PostgresqlStringArray extends PostgresqlArray<String>
	{
		public PostgresqlStringArray(Collection<String> list)
		{
			super(list);
		}

		public String getBaseTypeName() throws SQLException
		{
			return "text";
		}

		public int getBaseType() throws SQLException
		{
			return java.sql.Types.VARCHAR;
		}

		protected String toString(String t)
		{
			return t;
		}
	}

	public final static class PostgresqlIntegerArray extends PostgresqlArray<Integer>
	{
		public PostgresqlIntegerArray(Collection<Integer> list)
		{
			super(list);
		}

		public String getBaseTypeName() throws SQLException
		{
			return "int";
		}

		public int getBaseType() throws SQLException
		{
			return java.sql.Types.INTEGER;
		}

		protected String toString(Integer t)
		{
			return String.valueOf(t);
		}
	}
	
	public final static class PostgresqlLongArray extends PostgresqlArray<Long>
	{
		public PostgresqlLongArray(Collection<Long> list)
		{
			super(list);
		}

		public String getBaseTypeName() throws SQLException
		{
			return "int8";
		}

		public int getBaseType() throws SQLException
		{
			return java.sql.Types.BIGINT;
		}

		protected String toString(Long t)
		{
			return String.valueOf(t);
		}
	}
}
